メールサーバーへの不正アクセス対策

投稿日:

ya1802_2020.jpg


サーバーへの不正アクセス対策

概要:ここのサイトを参考にしました

/var/log/maillogから「authentication failed」「disconnect from unknown」「from unknown」の行の中のIPを取り出し、そのIPをiptablesコマンドでドロップする..という方法です。日に一回、以下のシェルスクリプトを実行させることで段々と減少しているのが分かります。(以下のシェルスクリプトは最後に記載しています) fail2ban の導入等、いろいろと施してみましたが、この方法が私的にはベストのように思われます。

手順は以下:

1: maillog より「authentication failed」のある行のIPを抽出し/var/log/blacklist-file に書き込み    ( /var/log/blacklist-file は任意の場所.任意名)

cat /var/log/mail/maillog | grep 'authentication failed' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file


2: maillog より「disconnect from unknown」のある行のIPを抽出し/var/log/blacklist-file に書き込み

cat /var/log/mail/maillog | grep 'disconnect from unknown' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file


3: maillog より「from unknown」のある行のIPを抽出し/var/log/blacklist-file に書き込み

cat /var/log/mail/maillog | grep 'from unknown' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file


4:blacklist-file(IP アドレス) を昇順にソート

sort -V /var/log/blacklist-file > /var/log/blacklist-sort_file

5: 重複している IP をカット

uniq /var/log/blacklist-sort_file /var/log/blacklist-sort_file2


6: 最後に、作成した /var/log/blacklist-sort_file2 を /etc/firewalld/blacklist-file に置き換える  

rm -f /var/log/blacklist-file

 

cp -p /var/log/blacklist-sort_file2 /var/log/blacklist-file

 

rm -f /var/log/blacklist-sort_file

 

rm -f /var/log/blacklist-sort_file2


7: iptablesコマンドでドロップ

while read LINE
do
 iptables -A INPUT -s $LINE -j LOG --log-prefix "[Iptables DROP_SASL_FAIL]" || exit 1
 iptables -A INPUT -s $LINE -j DROP
 done < /var/log/blacklist-file
exit $?


シェルスクリプト

#!/bin/bash

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

#: maillog より IP address 抽出

cat /var/log/mail/maillog | grep 'authentication failed' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file

#: maillog より IP address 抽出

cat /var/log/mail/maillog | grep 'disconnect from unknown' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file

#: maillog より IP address 抽出

cat /var/log/mail/maillog | grep 'from unknown' | egrep -o -e '\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]' | sed -e 's/^\[\(.*\)\]$/\1/g' | sort | uniq -c | awk '$1 > 1 {print $2}' >> /var/log/blacklist-file

#: maillog をcrean

cd /var/log/mail

: > maillog

cd

#:blacklist-file(IP アドレス) を昇順にソート

sort -V /var/log/blacklist-file > /var/log/blacklist-sort_file

#:重複している IP をカット

uniq /var/log/blacklist-sort_file /var/log/blacklist-sort_file2

#:作成した /var/log/blacklist-sort_file2 を /etc/firewalld/blacklist-file に置き換える

rm -f /var/log/blacklist-file

cp -p /var/log/blacklist-sort_file2 /var/log/blacklist-file

rm -f /var/log/blacklist-sort_file

rm -f /var/log/blacklist-sort_file2

#:iptablesコマンドでドロップ

while read LINE
do
 iptables -A INPUT -s $LINE -j LOG --log-prefix "[Iptables DROP_SASL_FAIL]" || exit 1
 iptables -A INPUT -s $LINE -j DROP
 done < /var/log/blacklist-file
exit $?